package com.dfbz.springboot_project.realm;

import com.dfbz.springboot_project.entity.AdminInfo;
import com.dfbz.springboot_project.service.AdminInfoService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    AdminInfoService adminInfoService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("222");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        AdminInfo adminInfo = adminInfoService.selectByName(username);
        if (adminInfo==null){
            return null;
        }
        return new SimpleAuthenticationInfo(username,adminInfo.getAdminPassword(), ByteSource.Util.bytes(username),getName());
    }
}
